package com.fpt.edu.vn.h2tv.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.fpt.edu.vn.h2tv.dao.EntityManagerHelper;
import com.fpt.edu.vn.h2tv.dao.ITbBookDAO;
import com.fpt.edu.vn.h2tv.dao.TbAccountDAO;
import com.fpt.edu.vn.h2tv.dao.TbBookDAO;
import com.fpt.edu.vn.h2tv.dao.TbCommentDAO;
import com.fpt.edu.vn.h2tv.dao.entity.TbBook;
import com.fpt.edu.vn.h2tv.dao.entity.TbComment;
import com.fpt.edu.vn.h2tv.dto.BookDetail;
import com.fpt.edu.vn.h2tv.dto.Comment;
import com.fpt.edu.vn.h2tv.exception.ExceptionNotFound;

public class BookDetailService{

    
    public static Object getBookDetail(int bookId){
        ITbBookDAO dao = new TbBookDAO();
        TbBook book = dao.findById(bookId);
        if(book==null){
            throw new ExceptionNotFound("Can not find this book");
        }
        BookDetail detail = new BookDetail();
        detail.setBooktid(book.getBooktid());
        detail.setAuthor(book.getAuthor());
        detail.setBookname(book.getBookname());
        //detail.setDaterelease(book.getDaterelease().toString());
        detail.setDescription(book.getDescription());
        detail.setPrice(book.getPrice());
        detail.setBookImage(book.getBookImagel());
        List<Comment> comments = new ArrayList<Comment>();
        for (TbComment tbComment : book.getTbComments()) {
            Comment comment = new Comment();
            comment.setCommentId(tbComment.getCommentid());
            comment.setContent(tbComment.getComment());
            comment.setCreateTime(tbComment.getCreatetime().toString());
            comment.setUserName(tbComment.getTbAccount().getUsername());
            comment.setUserAvatar(tbComment.getTbAccount().getAvatar());
            comments.add(comment);
        }
       detail.setComment(comments); 
         book.setCount(book.getCount().longValue()+1);
         EntityManagerHelper.beginTransaction();
         dao.update(book);
         EntityManagerHelper.commit();
        return detail;
    }
    
    public static Comment addComment(int bookId, int userId, String commentText){
        TbBookDAO bookDAO = new TbBookDAO();
        TbCommentDAO commentDAO = new TbCommentDAO();
        TbAccountDAO accountDao = new TbAccountDAO();
        TbComment tbComment = new TbComment();
        tbComment.setComment(commentText);
        tbComment.setCreatetime(new Timestamp(System.currentTimeMillis()));
        tbComment.setTbBook(bookDAO.findById(bookId));
        tbComment.setTbAccount(accountDao.findById((long)userId));
        //tbComment.setCommentid(11);
        EntityManagerHelper.beginTransaction();
        TbComment result = commentDAO.save(tbComment);
        System.out.println(result.getCommentid());
        System.err.println(result.getCreatetime());
        EntityManagerHelper.commit();
        
        Comment comment = new Comment();
        comment.setContent(tbComment.getComment());
        comment.setCreateTime(tbComment.getCreatetime().toString());
        comment.setUserName(tbComment.getTbAccount().getUsername());
        comment.setUserAvatar(tbComment.getTbAccount().getAvatar());
        return comment;
    }
    
    public static void main(String...args){
        BookDetailService.addComment(1, 1, "day la comment test");
    }

}
